package com.fhz.kotlinmvvm.utils

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.fhz.kotlinmvvm.api.FengServiceApi
import com.fhz.kotlinmvvm.entity.Banner
import javax.inject.Inject

/**
 * 时间:2023/9/18
 * @author Mr.Feng
 * 简述: TODO
 */
class ExamplePagingSource @Inject constructor(val serviceApi: FengServiceApi) : PagingSource<Int, Banner>() {


    override fun getRefreshKey(state: PagingState<Int, Banner>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Banner> {
        val banners = serviceApi.getBanners(0, 1, 5)
        return LoadResult.Page(banners.data.list,null,null)
    }
}